package com.gy.zpchat.ui.chat.group;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

import im.zego.zim.callback.ZIMGroupMemberListQueriedCallback;
import im.zego.zim.callback.ZIMGroupOwnerTransferredCallback;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.entity.ZIMGroupMemberInfo;
import im.zego.zim.entity.ZIMGroupMemberQueryConfig;
import im.zego.zim.enums.ZIMErrorCode;
import com.gy.zpchat.R;
import com.gy.zpchat.SDKManager;
import com.gy.zpchat.enums.MessageLoadType;
import com.gy.zpchat.ui.BaseActivity;
import com.gy.zpchat.ui.chat.group.adapter.GroupOwnerTransferAdapter;

public class ZIMGroupOwnerTransferActivity extends BaseActivity {

    private static final String TAG = "ZIMGroupMemberDeleteActivity";
    private String groupID;
    private int QUERY_MEMBER_COUNT = 30;
    private int queryNextFlag;
    private MessageLoadType messageLoadType;

    private GroupOwnerTransferAdapter ownerTransferAdapter = new GroupOwnerTransferAdapter();

    public static void actionStart(Activity activity, String groupID) {
        Intent intent = new Intent(activity, ZIMGroupOwnerTransferActivity.class);
        intent.putExtra("groupID", groupID);
        activity.startActivity(intent);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_member_delete);
        RecyclerView recyclerView = findViewById(R.id.member_selected_list);

        groupID = getIntent().getStringExtra("groupID");

        TextView titleTextView = findViewById(R.id.title);
        titleTextView.setText(getString(R.string.select_new_owner));

        Button button = findViewById(R.id.button);
        button.setText(getString(R.string.ok));

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(ownerTransferAdapter);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();
                if (lastVisibleItemPosition + 1 == ownerTransferAdapter.getItemCount()) {
                    if (messageLoadType != MessageLoadType.MESSAGE_LOAD_TYPE_LOADING && messageLoadType != MessageLoadType.MESSAGE_LOAD_TYPE_NO_DATA) {
                        queryMemberList();
                    }
                }
            }
        });

        queryMemberList();

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void queryMemberList() {
        messageLoadType = MessageLoadType.MESSAGE_LOAD_TYPE_LOADING;
        ZIMGroupMemberQueryConfig groupMemberQueryConfig = new ZIMGroupMemberQueryConfig();
        groupMemberQueryConfig.count = QUERY_MEMBER_COUNT;
        groupMemberQueryConfig.nextFlag = queryNextFlag;
        SDKManager.sharedInstance().queryGroupMemberList(groupID, groupMemberQueryConfig, new ZIMGroupMemberListQueriedCallback() {
            @Override
            public void onGroupMemberListQueried(String groupID, ArrayList<ZIMGroupMemberInfo> userList, int nextFlag, ZIMError errorInfo) {

                ownerTransferAdapter.addAll(userList);
                queryNextFlag = nextFlag;
                if (nextFlag == 0) {
                    messageLoadType = MessageLoadType.MESSAGE_LOAD_TYPE_NO_DATA;
                } else {
                    messageLoadType = MessageLoadType.MESSAGE_LOAD_TYPE_LOAD_COMPLETE;
                }
            }
        });
    }

    public void onUpdateGroupMember(View view) {
        SDKManager.sharedInstance().transferGroupOwner(ownerTransferAdapter.selectedMember, groupID, new ZIMGroupOwnerTransferredCallback() {
            @Override
            public void onGroupOwnerTransferred(String groupID, String toUserID, ZIMError errorInfo) {
                
                if (errorInfo.code != ZIMErrorCode.SUCCESS) {
                    Toast.makeText(ZIMGroupOwnerTransferActivity.this, "Failed to set group member role error: " + errorInfo.code.name(), Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(ZIMGroupOwnerTransferActivity.this, "Operation success", Toast.LENGTH_LONG).show();
                    finish();
                }
            }
        });
    }

}
